package com.oraking.android.cmm.view;

import android.content.Context;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.oraking.android.cmm.R;

/**
 * 파일명          : EditTextView.java 
 * 작성일          : 2011. 9. 29.
 * 작성자          : <a href='http://oraking.springnote.com/' target='_blank'>Teddy</a>
 * Todo      : X표시를 위한 EditTextView
 * 수정내용
 * -------------------------------------------------------------
 *      수정일      |      수정자      |              수정내용              
 * -------------------------------------------------------------
 *              |              |
 */
public class EditTextView extends LinearLayout{
	
	private View view;
	private EditText editText;
	private ImageButton imgBtn;
	private String hintString;
	
	public EditTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	
	public EditTextView(Context context) {
		super(context);
		init();
	}

	/**
	 * @title  : 
	 * @location : com.oraking.android.cmm.view.EditTextView.init
	 * @writeDay : 2011. 9. 29. 오후 3:30:03
	 * @Todo  : EditTextView 초기화 하는 Method
	 */
	private void init(){
		LayoutInflater li = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		view = li.inflate(R.layout.edittextview, this, false);		
		addView(view);
		
		editText = (EditText) findViewById(R.id.searchText);
		imgBtn = (ImageButton) findViewById(R.id.clear_btn);
		
			
		// EditText에 Text의 변화를 통해 X표시
		editText.addTextChangedListener(new TextWatcher() {	
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
			}
			
			public void beforeTextChanged(CharSequence s, int start, int count,	int after) {
				// TODO Auto-generated method stub
			}
			
			public void afterTextChanged(Editable s) {
				if(editText.getText().length()==1){
					imgBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.icon_select_off));
					imgBtn.setVisibility(View.VISIBLE);
				}else if(editText.getText().length()==0){
					imgBtn.setVisibility(View.INVISIBLE);
				}
			}
		});
		
		// X표시 버튼을 이용해서 EditText에서 내용 지우기
		imgBtn.setOnTouchListener(new OnTouchListener() {
			
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {   
				    // when you press it down, it's gonna swap to a sort of "selected" image   
					imgBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.icon_select_on));   
				} else if (event.getAction() == MotionEvent.ACTION_UP) {   
				    // when you release the button the text will be cleared up and then disappear right afterward   
					editText.setText("");
				    imgBtn.setVisibility(View.INVISIBLE);   
				}   
				return false;
			}
		});
	}
	
	/**
	 * @title  : 
	 * @location : com.oraking.android.cmm.view.EditTextView.setHintString
	 * @writeDay : 2011. 9. 29. 오후 3:30:03
	 * @Todo  : Hint 설정하는 Method
	 */
	public void setHintString(String hintString) {
		this.hintString = hintString;
		editText.setHint(this.hintString);
	}
	
	/**
	 * @title  : 
	 * @location : com.oraking.android.cmm.view.EditTextView.setInputType
	 * @writeDay : 2011. 9. 29. 오후 3:30:03
	 * @Todo  : 숫자만 입력하도록 하는 Method
	 */
	public void setInputType(String type){
		if("number".equals(type)){
			this.editText.setInputType(InputType.TYPE_CLASS_NUMBER);	
		}
	}
	
	/**
	 * @title  : 
	 * @location : com.oraking.android.cmm.view.EditTextView.getText
	 * @writeDay : 2011. 9. 29. 오후 3:30:03
	 * @Todo  : 입력한 Text 가져오는 Method
	 */
	public String getText(){
		return editText.getText().toString();
	}
}
